let App = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataType: 1,
    list: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
 
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 获取订单列表
    this.getMoneyList();
  },

  /**
   * 获取订单列表
   */
  getOrderList: function (dataType) {
    let _this = this;
    App._get('user/tixianlist', { dataType, is_tuan: 0 }, function (result) {
      var list = result.data.list;
      for(var i=0;i<list.length;i++){
        if(list[i].state==1){
          list[i].state ="已审核";
        }else if(list[i].state==0){
          list[i].state = "审核中";
        }
      }
      console.log("list",list);
      _this.setData({ list: list});
      result.data.list.length && wx.pageScrollTo({
        scrollTop: 0
      });
    });
  },
  getMoneyList:function(){
    let _this = this;
    App._get('user/mymoneylist', {  }, function (result) {
      var list = result.data.list;
      
      console.log("list", list);
      _this.setData({ list: list });
      result.data.list.length && wx.pageScrollTo({
        scrollTop: 0
      });
    });
  },

  /**
   * 切换标签
   */
  bindHeaderTap: function (e) {
    console.log("e.target.dataset.type", e.target.dataset.type);
    this.setData({ dataType: e.target.dataset.type });
    // 获取订单列表
    if(e.target.dataset.type=="1"){
      this.getMoneyList();
    }else{
      this.getOrderList(e.target.dataset.type);
    }
    
  },

  /**
   * 取消订单
   */
  cancelOrder: function (e) {
    let _this = this;
    let order_id = e.currentTarget.dataset.id;
    wx.showModal({
      title: "提示",
      content: "确认取消订单？",
      success: function (o) {
        if (o.confirm) {
          App._post_form('user.order/cancel', { order_id }, function (result) {
            _this.getOrderList(_this.data.dataType);
          });
        }
      }
    });
  },

  /**
   * 确认收货
   */
  receipt: function (e) {
    let _this = this;
    let order_id = e.currentTarget.dataset.id;
    wx.showModal({
      title: "提示",
      content: "确认收到商品？",
      success: function (o) {
        if (o.confirm) {
          App._post_form('user.order/receipt', { order_id }, function (result) {
            _this.getOrderList(_this.data.dataType);
          });
        }
      }
    });
  },

  /**
   * 发起付款
   */
  payOrder: function (e) {
    let _this = this;
    let order_id = e.currentTarget.dataset.id;

    // 显示loading
    wx.showLoading({ title: '正在处理...', });
    App._post_form('user.order/pay', { order_id }, function (result) {
      if (result.code === -10) {
        App.showError(result.msg);
        return false;
      }
      // 发起微信支付
      // console.log("timeStamp", result.data.timeStamp);
      // console.log("nonceStr", result.data.nonceStr);
      // console.log("prepay_id", result.data.prepay_id);
      // console.log("timeStamp", result.data.timeStamp);
      console.log("paySign", result.data.paySign);

      wx.requestPayment({
        timeStamp: result.data.timeStamp,
        nonceStr: result.data.nonceStr,
        package: 'prepay_id=' + result.data.prepay_id,
        signType: "RSA",
        paySign: result.data.paySign,
        success: function (res) {
          // 跳转到已付款订单
          App.showError('支付成功', function () {

          });
        },
        fail: function () {
          App.showError('订单未支付');
        },
      });
    });
  },

  /**
   * 跳转订单详情页
   */
  detail: function (e) {
    let order_id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../order/detail?order_id=' + order_id
    });
  },

  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  }


});